const express = require("express");
const router = express.Router();
const bcrypt = require("bcryptjs");
const { PrismaClient } = require("@prisma/client");

const prisma = new PrismaClient({
  log: ["query", "info", "warn"],
});

/**
 * 用户列表
 */
router.get("/", async (req, res, next) => {
  let users = await prisma.sys_user.findMany();
  res.render("user/index", {
    title: "用户列表",
    users: users,
  });
});

router.post("/", async (req, res, next) => {
  let user = req.body;
  user.password = bcrypt.hashSync(user.password, 10);
  await prisma.sys_user.create({
    data: user,
  });
  res.redirect("/user");
});

router.get("/:id", async (req, res, next) => {
  let role = await prisma.sys_user_role.delete({
    where: {
      id: parseInt(req.params.id),
    },
    disconnect: this.true,
  });
  res.redirect("/user");
});

module.exports = router;
